;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.AgentTerminalOutbound", [])
        .controller("AgentTerminalOutboundCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", "$stateParams", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService, $stateParams) {
            // tab标签展示布尔值
            $scope.activeFlag = [true, false, false];
            var pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            // 分页信息
            $scope.pageInfo = angular.copy(pageInfo);
            // 导出功能信息
            $scope.exportParam = {
                url: ybHttp.RequestUrl("device", $scope.isCHY ? '/report/exportAgentTerminalSalesByOutbound4Cy' : "/report/exportAgentTerminalSalesByOutbound"),
            };
            // 搜索条件
            $scope.condition = {};
            // 数据列表
            $scope.dataList = [];
            // 总计的数据
            $scope.totalRow = [];
            // 高级搜索和配置表格项的展示与隐藏
            $scope.toolbarConfig = {
                showAgentColConfig: false,
                showAgentAdSearch: false,
            };


            var TdWidth = 120; // 表格列宽度
            $scope.tbConf = {
                tableCol: [
                    {
                        name: "三包",
                        dataField: "threePacks",
                        isChecked: true,
                        value: 1,
                        width: TdWidth,
                        isShow: true
                    },
                    {
                        name: "自用",
                        dataField: "selfUse",
                        width: TdWidth,
                        isChecked: !$scope.isCHY,
                        value: 2,
                        isShow: !$scope.isCHY
                    },
                    {
                        name: "现金销售",
                        dataField: "cashSales",
                        width: TdWidth,
                        isChecked: true,
                        value: 3,
                        isShow: true
                    },
                    {
                        name: "赠送",
                        dataField: "gift",
                        width: TdWidth,
                        isChecked: true,
                        value: 4,
                        isShow: true
                    },
                    {
                        name: "正常损耗",
                        dataField: "normalLoss",
                        width: TdWidth,
                        isChecked: !$scope.isCHY,
                        value: 5,
                        isShow: !$scope.isCHY
                    },
                    {
                        name: "其他",
                        dataField: "other",
                        width: TdWidth,
                        isChecked: true,
                        value: 6,
                        isShow: true
                    },
                    {
                        name: "普通销售",
                        dataField: "commonSale",
                        width: TdWidth,
                        isChecked: $scope.isCHY,
                        value: 8,
                        isShow: $scope.isCHY
                    },
                    {
                        name: "小计",
                        dataField: "subtotal",
                        width: TdWidth,
                        isChecked: true,
                        value: 7,
                        isShow: true
                    }
                ],
                tableTopHead: [{name: '出库金额（按类型）', isShow: true}]
            };

            $scope.searchCol = $scope.tbConf.tableCol.filter(function(item) {
                return item.isShow;
            });

            setTopHead($scope.searchCol.length);

            var FirstThWidth = 200; // 第一个表格头宽度
            $scope.agentTbCol = [
                {
                    type: "",
                    name: "代理商名称",
                    dataField: "agentName",
                    width: FirstThWidth,
                    isShow: true,
                }
            ];

            $scope.selectClassInfo = {
                notCheckedValues: [],
                selectClassesStr: ''
            };

            $scope.datePickerFlag = [false, false];

            // 检查日期
            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            // 切换时间展示 隐藏
            $scope.switchDataPicker = function (index) {
                $scope.datePickerFlag = [false, false];
                if (angular.isDefined(index)) {
                    $scope.datePickerFlag[index] = true;
                }
            };

            // 重置搜索条件
            $scope.reset = function () {
                $scope.condition = {};
                $scope.selectClassInfo = {
                    notCheckedValues: [],
                    selectClassesStr: ''
                };
                // 恢复顶部表格头原有宽度和colspan
                setTopHead($scope.searchCol.length);
                // 恢复原有表格头的展示
                angular.forEach($scope.tbConf.tableCol, function (item) {
                    if ($scope.searchCol.find(col=> col.value == item.value)) {
                        item.isShow = true;
                        item.isChecked = true;
                    }
                });
            };

            // 搜索供货关系列表
            $scope.search = function (curPage) {
                getDataList(curPage);
            };

            // 切换高级搜索和表格配置展示与隐藏
            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig[toolbar]) {
                    $scope.toolbarConfig[toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig[toolbar] = true;
                    });
                }
            };

            // 高级搜索中的选择分类
            $scope.selectClass = function () {
                var checkedClass = []; // 已选中的分类
                var notCheckedValues = [];
                angular.forEach($scope.searchCol, function (item) {
                    if (item.isChecked) {
                        checkedClass.push(item.name);
                    } else {
                        notCheckedValues.push(item.value);
                    }
                });
                $scope.selectClassInfo.selectClassesStr = checkedClass.join(',');
                $scope.selectClassInfo.notCheckedValues = notCheckedValues;
            };

            // 高级搜索中的搜索
            $scope.confirmSearch = function () {
                var notCheckLength = $scope.selectClassInfo.notCheckedValues.length;
                var searchLength = $scope.searchCol.length;
                if (notCheckLength === searchLength) {
                    ybWidget.ShowMessage("类型不能全部置为空！", MESG_TYPE.Warn);
                    return;
                }
                var checkLength = 0;
                var showTypeList = [];
                angular.forEach($scope.tbConf.tableCol, function (item) {
                    if ($scope.searchCol.find(col=> col.value == item.value)) {
                        if ($scope.selectClassInfo.notCheckedValues.indexOf(item.value) >= 0) {
                            item.isShow = false;
                        } else {
                            item.isShow = true;
                            showTypeList.push(item.value);
                            checkLength++;
                        }
                    }
                });
                $scope.condition.showTypeList = showTypeList;
                setTopHead(checkLength);
                $scope.search(1);
            };

            // 设置表格头最顶部的宽度和colspan
            function setTopHead (num) {
                $scope.tbConf.scrollWidth = FirstThWidth + num*TdWidth;
                $scope.tbConf.tableTopHead[0].colSpan = num;
                $scope.tbConf.tableTopHead[0].width = num*TdWidth;
            }

            // 获取代理商满意度列表
            function getDataList(curPage) {
                var reqUrl = $scope.isCHY ? '/report/agentTerminalSalesByOutbound4Cy' : "/report/agentTerminalSalesByOutbound";
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                //遍历搜索条件对象里的空值并删除  不然搜索不出数据
                Object.keys($scope.condition).forEach(function (key) {
                    if (!$scope.condition[key] && $scope.condition[key] !== 0) delete $scope.condition[key];
                });
                var param = {
                    pageNum: $scope.pageInfo.curPage,
                    pageSize: $scope.pageInfo.pageSize,
                    filters: angular.copy($scope.condition)
                };
                ybHttp.Post('device', reqUrl, param).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    $scope.totalRow = res.value.list.splice(0, 1);
                    $scope.pageInfo.totalRows = res.value.total;
                    $scope.pageInfo.totalPages = res.value.pages;
                    $scope.dataList = res.value.list;
                });
            }

            // 按钮权限code
            var btnPermissionCode = {
                agentTerminalOutboundExport: window.ActionCode.agentTerminalOutboundExport
            };

            // 按钮权限
            $scope.btnPermission = {
                agentTerminalOutboundExport: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            (function () {
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])
    ;
})(window, window.angular);